<?php

/**
 * @author phpadmin
 * @copyright 2012
 */

class Product_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function get_all_stored_item()
    {
        $this->load->database();
        
        $sql = "SELECT * FROM cs_store ";
        $sql .= "LEFT JOIN cs_product_state ON cs_product_state.cs_product_state_id = cs_store.cs_product_state_id ";
        $sql .= "LEFT JOIN cs_product_model ON cs_product_model.cs_product_id = cs_store.cs_product_id ";
        $sql .= "LEFT JOIN cs_vendor ON cs_vendor.cs_vendor_id = cs_store.cs_vendor_id ";
        $sql .= "WHERE cs_store.cs_product_state_id = 1;";
        
        $query = $this->db->query($sql);
        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }
    
    public function get_all_product_list()
    {
        $this->load->database();
        
        $sql = "SELECT * FROM cs_product_model ";
        $sql .= "LEFT JOIN cs_vendor ON cs_vendor.cs_vendor_id = cs_product_model.cs_vendor_id ";
        $sql .= ";";
        
        $query = $this->db->query($sql);
        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }

    public function get_all_category()
    {
        $this->load->database();
        
        $sql = "SELECT * FROM cs_category ";
        $sql .= ";";
        
        $query = $this->db->query($sql);
        if($query->num_rows() > 0)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }

    public function add_new_vendor_db()
    {
        $this->load->database();
        
        $data = array(
                    'cs_vendor_name' => $this->input->post('vendor'),
                    'cs_vendor_addr' => $this->input->post('addr'),
                    'cs_last_change' => DATE("Y-m-d H:i:s"),
                    );
                
        $this->db->insert('cs_vendor', $data);
        
    }
    
    public function new_product_db()
    {
        $this->load->database();
        
        $data = array(
                    'cs_vendor_id' => $this->input->post('vendor'),
                    'cs_category_id' => $this->input->post('category'),
                    'cs_product_model' => $this->input->post('productmodel'),
                    'cs_product_available' => 0,
                    'cs_product_cost' => 0.0,
                    'cs_last_change' => DATE("Y-m-d H:i:s"),
                    );
                
        $this->db->insert('cs_product_model', $data);
        
    }
    
    public function add_product_store()
    {
        $this->load->database();
        
        $tmp = $this->input->post('productmodel');
        list($pid, $vid) = split('S', $tmp);
        
        $num = $this->input->post('quantity');
        $price = ceil($this->input->post('productprice') / $num);
        
        for($i = 0; $i < $num; $i++)
        {
            $data = array(
                        'cs_product_id' => $pid,
                        'cs_vendor_id' => $vid,
                        'cs_product_state_id' => 1,
                        'cs_product_quantity' => 1,
                        'cs_product_buying_price' => $price,
                        'cs_last_change' => DATE("Y-m-d H:i:s"),
                        );
                    
            $this->db->insert('cs_store', $data);
        }
        
        // TODO: need to update the cs_product_model table
    
    }
    
}